import { get, post, put, del } from '@/utils/request';

/**
 * 用户管理API服务
 */

/**
 * 获取用户列表
 */
export async function getUserList(params: USER.UserListParams): Promise<USER.UserListResponse> {
  return get('/admin/users', params);
}

/**
 * 获取用户详情
 */
export async function getUserDetail(id: number): Promise<USER.UserDetailResponse> {
  return get(`/admin/users/${id}`);
}

/**
 * 创建用户
 */
export async function createUser(data: USER.CreateUserParams): Promise<USER.ApiResponse<USER.UserModel>> {
  return post('/admin/users', data);
}

/**
 * 更新用户
 */
export async function updateUser(id: number, data: USER.UpdateUserParams): Promise<USER.ApiResponse<USER.UserModel>> {
  return put(`/admin/users/${id}`, data);
}

/**
 * 删除用户
 */
export async function deleteUser(id: number): Promise<USER.ApiResponse<null>> {
  return del(`/admin/users/${id}`);
}

/**
 * 批量操作用户
 */
export async function batchOperateUsers(data: USER.BatchOperationParams): Promise<USER.ApiResponse<null>> {
  return post('/admin/users/batch', data);
}

/**
 * 重置用户密码
 */
export async function resetUserPassword(id: number, newPassword: string): Promise<USER.ApiResponse<null>> {
  return post(`/admin/users/${id}/reset-password`, { password: newPassword });
}

/**
 * 获取用户统计数据
 */
export async function getUserStatistics(): Promise<USER.ApiResponse<USER.UserStatistics>> {
  return get('/admin/statistics/users');
}

/**
 * 导出用户数据
 */
export async function exportUsers(params: USER.UserListParams & { format: 'excel' | 'csv' }): Promise<Blob> {
  return get('/admin/users/export', params, { responseType: 'blob' });
}

/**
 * 获取用户角色选项
 */
export async function getUserRoleOptions(): Promise<USER.ApiResponse<Array<{ label: string; value: string }>>> {
  return get('/admin/users/role-options');
}

/**
 * 获取用户状态选项
 */
export async function getUserStatusOptions(): Promise<USER.ApiResponse<Array<{ label: string; value: string }>>> {
  return get('/admin/users/status-options');
}

/**
 * 检查用户名是否可用
 */
export async function checkUsernameAvailable(username: string): Promise<USER.ApiResponse<{ available: boolean }>> {
  return get('/admin/users/check-username', { username });
}

/**
 * 检查邮箱是否可用
 */
export async function checkEmailAvailable(email: string): Promise<USER.ApiResponse<{ available: boolean }>> {
  return get('/admin/users/check-email', { email });
}

/**
 * 获取用户学习进度
 */
export async function getUserLearningProgress(id: number): Promise<USER.ApiResponse<any>> {
  return get(`/admin/users/${id}/learning-progress`);
}

/**
 * 获取用户操作日志
 */
export async function getUserOperationLogs(id: number, params?: { page?: number; limit?: number }): Promise<USER.ApiResponse<any>> {
  return get(`/admin/users/${id}/operation-logs`, params);
}

/**
 * 发送用户通知
 */
export async function sendUserNotification(id: number, data: { title: string; content: string; type?: string }): Promise<USER.ApiResponse<null>> {
  return post(`/admin/users/${id}/send-notification`, data);
}

/**
 * 获取用户成就列表
 */
export async function getUserAchievements(id: number): Promise<USER.ApiResponse<any[]>> {
  return get(`/admin/users/${id}/achievements`);
}

/**
 * 为用户添加成就
 */
export async function addUserAchievement(id: number, achievementId: number): Promise<USER.ApiResponse<null>> {
  return post(`/admin/users/${id}/achievements`, { achievement_id: achievementId });
}

/**
 * 移除用户成就
 */
export async function removeUserAchievement(id: number, achievementId: number): Promise<USER.ApiResponse<null>> {
  return del(`/admin/users/${id}/achievements/${achievementId}`);
}

/**
 * 调整用户积分
 */
export async function adjustUserPoints(id: number, data: { points: number; reason: string }): Promise<USER.ApiResponse<null>> {
  return post(`/admin/users/${id}/adjust-points`, data);
}

/**
 * 调整用户经验值
 */
export async function adjustUserExperience(id: number, data: { experience: number; reason: string }): Promise<USER.ApiResponse<null>> {
  return post(`/admin/users/${id}/adjust-experience`, data);
}

/**
 * 获取用户登录历史
 */
export async function getUserLoginHistory(id: number, params?: { page?: number; limit?: number }): Promise<USER.ApiResponse<any>> {
  return get(`/admin/users/${id}/login-history`, params);
}

/**
 * 强制用户下线
 */
export async function forceUserLogout(id: number): Promise<USER.ApiResponse<null>> {
  return post(`/admin/users/${id}/force-logout`);
}

/**
 * 获取用户设备信息
 */
export async function getUserDevices(id: number): Promise<USER.ApiResponse<any[]>> {
  return get(`/admin/users/${id}/devices`);
}

/**
 * 解绑用户设备
 */
export async function unbindUserDevice(id: number, deviceId: string): Promise<USER.ApiResponse<null>> {
  return del(`/admin/users/${id}/devices/${deviceId}`);
}
